import useAppName from '@framework/useAppName'
import useConfig from '@framework/useConfig'
import autoImportVueRouter from '@framework/useAutoImport'
import * as VueRouter from 'vue-router'

// 整个工程的router，全局唯一
let router: VueRouter.Router

/**
 * useProjectRouter 全局路由
 * 依赖配置web.router.mode
 * @returns
 */
export default function useProjectRouter() {
  const config = useConfig<CoreConfig>()
  const appname = useAppName()
  if (router)
    return { router }

  router = VueRouter.createRouter({
    history: config.web.router.mode === 'history' ? VueRouter.createWebHistory(`/${appname}`) : VueRouter.createWebHashHistory(),
    routes: [
      {
        path: '/',
        redirect: '/index',
      },
      ...autoImportVueRouter(),
      {
        path: '/:pathMatch(.*)*',
        redirect: '/404',
      },
    ],
  })
  return { router }
}
